package cn.iocoder.yudao.adminserver.modules.live.controller.project;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.adminserver.modules.live.controller.project.vo.*;
import cn.iocoder.yudao.adminserver.modules.live.convert.project.LiveProjectConvert;
import cn.iocoder.yudao.adminserver.modules.live.dal.dataobject.project.LiveProjectDO;
import cn.iocoder.yudao.adminserver.modules.live.service.project.LiveProjectService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "项目")
@RestController
@RequestMapping("/live/project")
@Validated
public class LiveProjectController {

    @Resource
    private LiveProjectService projectService;

    @PostMapping("/create")
    @ApiOperation("创建项目")
    @PreAuthorize("@ss.hasPermission('live:project:create')")
    public CommonResult<Long> createProject(@Valid @RequestBody LiveProjectCreateReqVO createReqVO) {
        return success(projectService.createProject(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新项目")
    @PreAuthorize("@ss.hasPermission('live:project:update')")
    public CommonResult<Boolean> updateProject(@Valid @RequestBody LiveProjectUpdateReqVO updateReqVO) {
        projectService.updateProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('live:project:delete')")
    public CommonResult<Boolean> deleteProject(@RequestParam("id") Long id) {
        projectService.deleteProject(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('live:project:query')")
    public CommonResult<LiveProjectRespVO> getProject(@RequestParam("id") Long id) {
        LiveProjectDO project = projectService.getProject(id);
        return success(LiveProjectConvert.INSTANCE.convert(project));
    }

    @GetMapping("/list")
    @ApiOperation("获得项目列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('live:project:query')")
    public CommonResult<List<LiveProjectRespVO>> getProjectList(@RequestParam("ids") Collection<Long> ids) {
        List<LiveProjectDO> list = projectService.getProjectList(ids);
        return success(LiveProjectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得项目分页")
    @PreAuthorize("@ss.hasPermission('live:project:query')")
    public CommonResult<PageResult<LiveProjectRespVO>> getProjectPage(@Valid LiveProjectPageReqVO pageVO) {
        PageResult<LiveProjectDO> pageResult = projectService.getProjectPage(pageVO);
        return success(LiveProjectConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出项目 Excel")
    @PreAuthorize("@ss.hasPermission('live:project:export')")
    @OperateLog(type = EXPORT)
    public void exportProjectExcel(@Valid LiveProjectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LiveProjectDO> list = projectService.getProjectList(exportReqVO);
        // 导出 Excel
        List<LiveProjectExcelVO> datas = LiveProjectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目.xls", "数据", LiveProjectExcelVO.class, datas);
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取部门对应项目信息列表", notes = "获取部门对应项目信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "name", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "date", value = "2021/01/01", required = false, dataType = "string", paramType = "query")})
    public CommonResult<List<LiveProjectAndPlanRespVO>> getSimpleProject(Date date, String projectName) {
        if (null == date) {
            date = new Date();
        }
        return success(projectService.getSimpleProject(date, projectName));
    }


    public static void main(String[] args) {
        DateTime dateTime = DateUtil.beginOfYear(new Date());
        System.out.println("dateTime = " + dateTime);
    }
}
